package cn.freedomblue.login.controller;

import cn.freedomblue.login.domain.LoginInfo;
import cn.freedomblue.login.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(){
        return "static/index.html";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(){
        return "login";
    }

    @ResponseBody
    @RequestMapping(value = "/login/checking", method = RequestMethod.POST)
    public ModelAndView check(LoginInfo loginInfo){
        ModelAndView modelAndView = new ModelAndView("/login");
        if(loginService.checkLogin(loginInfo)) {
            ModelMap model = new ModelMap();
            model.addAttribute("user.name", loginInfo.getUsername());
            modelAndView = new ModelAndView("main", model);
        }
        return modelAndView;
    }


}
